cmake_minimum_required(VERSION 3.8)
project(loop_closure)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# 1. 查找所有依赖包
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(std_msgs REQUIRED)
find_package(lio_sam REQUIRED)
find_package(pcl_conversions REQUIRED)
find_package(Eigen3 3.3 REQUIRED NO_MODULE)

# 【核心修正-1】: 使用 COMPONENTS 关键字查找PCL
# 这会确保 PCL_INCLUDE_DIRS 和 PCL_LIBRARIES 变量被正确填充
find_package(PCL REQUIRED COMPONENTS common io)

# 2. 创建 Scan Context 核心库
add_library(scancontext_core src/Scancontext.cpp)

# 3. 【核心修正-2】: 为 scancontext_core 手动添加PCL的头文件路径
target_include_directories(scancontext_core PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  ${PCL_INCLUDE_DIRS} # <-- 使用PCL提供的变量
)

# 4. 【核心修正-3】: 将 scancontext_core 链接到 Eigen3 和 PCL的库文件
target_link_libraries(scancontext_core PUBLIC
  Eigen3::Eigen
  ${PCL_LIBRARIES} # <-- 使用PCL提供的变量
)

# 5. 创建主节点
add_executable(loop_closure_node src/loop_closure_node.cpp)

# 6. 为主节点添加ROS依赖
ament_target_dependencies(loop_closure_node PUBLIC
  rclcpp
  sensor_msgs
  std_msgs
  lio_sam
  pcl_conversions
)

# 7. 将主节点链接到我们自己的库
target_link_libraries(loop_closure_node PUBLIC
  scancontext_core
)

# 8. 安装
install(TARGETS
  loop_closure_node
  DESTINATION lib/${PROJECT_NAME}
)

install(
    DIRECTORY include/
    DESTINATION include
)

ament_package()